var currentDragItem;
$(document).ready(function () {
//	$("#allporkers").droppable({
//		tolerance: "intersect",
//		drop: function( event, ui ) {
//			console.log("ddddddsomething drop in allporkers");
//			if (currentDragItem.parent().attr("id") == "allporkers") {
				//说明就在我们自己内部，啥都不敢
//			} else {
				//看来在下面的绿色空间，移动上了
//				currentDragItem.detach();
//				currentDragItem.appendTo("#allporkers");
//			}
//		},
//		out: function( event, ui ) {
//			console.log("oooooosomething out of allporkers");
//		},
//		over: function( event, ui ) {
//			console.log("something just over allporkers");
//		}
//	});
	$("#gamehere").sortable({
//		revert: true,
		
		out: function( event, ui ) {
			console.log("ooooosomething remove of gamehere");
			//当把item移出sort list时，ui.helper为非null
			//我们仅删除那些加减乘除
			if(ui.helper != null && $(ui.item).hasClass("expcard")) {
				$(ui.item).remove();
			}
		},
		over: function( event, ui ) {
//			console.log("something just over gamehere");
		}
	});
	$(".porker").draggable({
		connectToSortable: "#gamehere",
//		snap: "#allporkers, #gamehere",
//		snapMode: "inner",
		drag: function( event, ui ) {
//			console.log("porker is draging");
		},
		start: function( event, ui ) {
//			console.log("porker is start to drag");
			currentDragItem = $(this);
		},
		stop: function( event, ui ) {
//			console.log("porker stop for drag");
//			currentDragItem = null;
		}
		
	});
	$(".expcard").draggable({
		connectToSortable: "#gamehere",
		helper: "clone",
//		revert: false,
//		snap: "#allporkers, #gamehere",
//		snapMode: "inner",
		drag: function( event, ui ) {
			console.log("porker is draging");
		},
		start: function( event, ui ) {
//			console.log("porker is start to drag");
			currentDragItem = $(this);
		},
		stop: function( event, ui ) {
			console.log("porker stop for drag");
		}
		
	});
	//hide modal dialog
	$( "#passDialog" ).dialog({
		autoOpen: false,
	      modal: true,
	      close: function( event, ui ) {
	    	  nextGame();
	      }
	});
	$( "#tryagainDialog" ).dialog({
		autoOpen: false,
	      modal: true
	});
	$( "#failDialog" ).dialog({
		autoOpen: false,
	      modal: true,
	      close: function( event, ui ) {
	    	  goHome();
	      }
	});
});

function getExp() {
	var list = $("#gamehere *");
	var oldp = $("#allporkers .porker");
	if (list.size() == 0) {
		alert("你好像都还没有玩呢");
	} else if(oldp.size() != 0){
		alert("还有扑克没有移下来");
	} else {
		//生成表达式
		var line = "";
		list.each(function(){
			line += $(this).attr("data");
		});
		console.log(line);
		$.ajax({
			url: requestContentPath + "/game/do24exp?24exp=" + encodeURIComponent(line),
			dataType: "json",
			success: function(data) {
				console.log(data);
				if(data.pass) {
					$("#passDialog").dialog("open");
				} else {
					if(data.tryNumber < 4) {
						$("#tryNumber").html(data.tryNumber);
						$("#tryagainDialog .message").html(data.message);
						$("#tryagainDialog").dialog("open");
					} else {
						$("#failDialog").dialog("open");
					}
				}
			},
			error: function (xhr, text) {
				alert("Error: " + text);
			}
		});
	}
}

function goHome() {
	window.location=requestContentPath;
}

function nextGame() {
	window.location=requestContentPath + "/game/start";
}
function closeTryAgainDialog() {
	$("#tryagainDialog").dialog("close");
}

function logout() {
	window.location=requestContentPath + "/player/logout";
}

function backToHome() {
	$.ajax({
		url: requestContentPath + "/game/cleanCurrent",
		dataType: "json",
		complete:function() {
			window.location=requestContentPath;
		}
		
	});
}

function re_open() {
	$.ajax({
		url: requestContentPath + "/game/cleanCurrent",
		dataType: "json",
		complete:function() {
			window.location=requestContentPath + "/game/start";
		}
		
	});
}